package pl.webapps.alarmme;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmUtils {
    public static final int ERR_ALARM_EXISTS = 100;
    public static final int ERR_ALARM_LIMIT_REACHED = 101;
    public static final int ALARM_ADDED_SUCCESSFULLY = 102;
    public static final int ERR_ALARM_UNKNOWN_ERROR = 110;
    public static final String INTENT_ALARM_TIME_HOUR = "intent_alarm_time_hour";
    public static final String INTENT_ALARM_TIME_MINUTE = "intent_alarm_time_minute";
    public static final String INTENT_ALARM_MESSAGE = "intent_alarm_message";
    private static final String LOG_TAG = "AlarmMe.AlarmUtils";
    private AlarmManager mAlarmManager;
    private Context mContext;

    // private List<PendingIntentWrapper> intents;
    public AlarmUtils(Context con) {
        mContext = con;
        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        // intents = LocalResourcesHandler.loadAlarmsFromFile();
        // if (intents == null) {
        // intents = new ArrayList<PendingIntentWrapper>();
        // }
    }

    public void turnOffAlarm(int hour, int minute) {
        Log.d(LOG_TAG, "turn OFF Alarm at time=" + hour + ":" + minute);
        Intent intent = new Intent(mContext, RingingActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pending = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.cancel(pending);
        // int pos = getIntentPositionAt(hour, minute);
        // PendingIntent alarm = null;
        // if (pos >= 0) {
        // alarm = intents.get(pos).getPendingItem();
        // if (alarm != null) {
        // mAlarmManager.cancel(alarm);
        // intents.remove(pos);
        // LocalResourcesHandler.saveAlarmsToFile(intents);
        // Log.d(LOG_TAG, "alarm cancelled, intents size=" + intents.size());
        // }
        // }
    }

    public void turnOffGlobalAlarm() {
        Log.d(LOG_TAG, "turn OFF GLOBAL Alarm");
        Intent intent = new Intent(mContext, RingingActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pending = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.cancel(pending);
    }

    public void turnOnAlarm(int hour, int minute) {
        Log.d(LOG_TAG, "turn ON Alarm at time=" + hour + ":" + minute);
        Calendar now = Calendar.getInstance();
        now.setTimeInMillis(System.currentTimeMillis());
        Calendar alarmDate = Calendar.getInstance();
        alarmDate.setTimeInMillis(System.currentTimeMillis());
        alarmDate.set(Calendar.HOUR_OF_DAY, hour);
        alarmDate.set(Calendar.MINUTE, minute);
        alarmDate.set(Calendar.SECOND, 0);
        final long oneDayOffset = 24 * 60 * 60 * 1000;
        if (now.after(alarmDate)) {
            alarmDate.setTimeInMillis(alarmDate.getTimeInMillis() + oneDayOffset);
        }
        Log.d(LOG_TAG, "SET to time: YY=" + alarmDate.get(Calendar.YEAR) + ",MM=" + alarmDate.get(Calendar.MONTH)
                + ",DD=" + alarmDate.get(Calendar.DAY_OF_MONTH) + ",hh=" + alarmDate.get(Calendar.HOUR_OF_DAY) + ",mm="
                + alarmDate.get(Calendar.MINUTE));
        Intent intent = new Intent(mContext, RingingActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(INTENT_ALARM_TIME_HOUR, alarmDate.get(Calendar.HOUR_OF_DAY));
        intent.putExtra(INTENT_ALARM_TIME_MINUTE, alarmDate.get(Calendar.MINUTE));
        PendingIntent pending = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // intents.add(new PendingIntentWrapper(pending,
        // alarmDate.get(Calendar.HOUR_OF_DAY) + ":"
        // + alarmDate.get(Calendar.MINUTE)));
        // LocalResourcesHandler.saveAlarmsToFile(intents);
        mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmDate.getTimeInMillis(), oneDayOffset, pending);
        // Log.d(LOG_TAG, "alarm added, intents size=" + intents.size());
    }

    public void turnOnAlarm(String content) {
        String onlyTime = content.substring(content.length() - 5);
        Log.d(LOG_TAG, "only time=" + onlyTime);
        String[] splittedTime = onlyTime.split(":");
        try {
            turnOnAlarm(Integer.parseInt(splittedTime[0]), Integer.parseInt(splittedTime[1]));
        } catch (NumberFormatException e) {
            Log.e(LOG_TAG, "cancelAlarm - number format exception while parsing alarm time=" + onlyTime);
            e.printStackTrace();
        }
    }

    public void turnOffAlarm(String content) {
        String onlyTime = content.substring(content.length() - 5);
        Log.d(LOG_TAG, "only time=" + onlyTime);
        String[] splittedTime = onlyTime.split(":");
        try {
            turnOffAlarm(Integer.parseInt(splittedTime[0]), Integer.parseInt(splittedTime[1]));
        } catch (NumberFormatException e) {
            Log.e(LOG_TAG, "cancelAlarm - number format exception while parsing alarm time=" + onlyTime);
            e.printStackTrace();
        }
    }
    // private int getIntentPositionAt(int hour, int minute) {
    // int position = -1;
    // for (Iterator<PendingIntentWrapper> iterator = intents.iterator();
    // iterator.hasNext();) {
    // ++position;
    // PendingIntentWrapper intent = iterator.next();
    // String[] time = intent.getIntentTime().split(":");
    // try {
    // if (Integer.parseInt(time[0]) == hour && Integer.parseInt(time[1]) ==
    // minute) {
    // return position;
    // }
    // } catch (NumberFormatException e) {
    // Log.e(LOG_TAG,
    // "getIntentAt - number format exception while parsing alarm time=" +
    // intent.getIntentTime());
    // e.printStackTrace();
    // }
    // }
    // return -1;
    // }
}
